package cn.net.zj.scan.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;
import cn.net.zj.scan.R;
import cn.net.zj.scan.api.HttpMethods;
import cn.net.zj.scan.bean.StateBean;
import cn.net.zj.scan.utils.SharedPreferencesUtils;
import cn.net.zj.scan.utils.ToastUtils;
import cn.net.zj.scan.utils.ZjConfig;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhujun
 * @date 2018/5/11 13:28
 * @desc
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.userName)
    EditText etUserName;

    @BindView(R.id.password)
    EditText etPassword;


    @BindView(R.id.bt_url)
    Button bt_url;

    @BindView(R.id.tv_url)
    TextView tv_url;

    String TAG = "登录调试信息: ";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }


    @OnClick(R.id.bt_url)
    public void url(View view) {
        if (etPassword.getText() != null && etPassword.getText().toString().trim().length() > 0) {
            ZjConfig.baseUrl = etPassword.getText().toString();
            tv_url.setText("当前地址：" + ZjConfig.baseUrl);
        } else {
            etPassword.setError("请填写密码");
            return;
        }
    }

    @Override
    public void init() {
        super.init();
        String userName = (String) SharedPreferencesUtils.getParam(this, ZjConfig.userName, "");

        String psd = (String) SharedPreferencesUtils.getParam(this, ZjConfig.userPassword, "");
        if (!userName.equals("")) {
            etUserName.setText(userName);
//            passwordString.setText(psd);
        }
        tv_url.setText("当前地址：" + ZjConfig.baseUrl);
//        etUserName.setText(ZjConfig.userName);
    }

    @OnClick(R.id.login)
    public void onclick(Button view) {

        if (etUserName.getText() != null && etUserName.getText().toString().trim().length() > 0) {

        } else {
            etUserName.setError("请填写用户名");
            return;
        }
        login(etUserName.getText().toString().trim());

    }

    private void loginForApp(final String userName) {


//        Observable<StateBean> observable = HttpMethods.getInstance().getFyApi().getNetDataWithUrl("http://www.wanandroid.com/tools/mockapi/3888/login_app");

        Observable<StateBean> observable = HttpMethods.getInstance().getFyApi().getNetDataWithUrl("http://www.wanandroid.com/tools/mockapi/3888/login_app");
        observable.subscribeOn(Schedulers.io())//切换到io线程请求网络
                .observeOn(AndroidSchedulers.mainThread())//回到主线程处理请求结果
                .subscribe(new Observer<StateBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "开始获取客户信息");
                        showProgress("请稍后");
                    }

                    @Override
                    public void onNext(final StateBean resBean) {

                        if (resBean.isFlag()) {
                            if (userName.equals(ZjConfig.userName)) {
                                ToastUtils.showToast("登录成功!");
                                SharedPreferencesUtils.setParam(LoginActivity.this, ZjConfig.userName, userName);
//                                if (psd.equals(ZjConfig.userPassword)){
//                                    SharedPreferencesUtils.setParam(LoginActivity.this,ZjConfig.userPassword,psd);
//                                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
//                                }else {
//                                    ToastUtils.showToast("密码错误!");
//                                }
                            } else {
                                ToastUtils.showToast("用户为注册!");
                            }

                        } else {
                            ToastUtils.showToast("" + resBean.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissProgress();
                        ToastUtils.showToast("请检查网络,再尝试登陆!");
                    }

                    @Override
                    public void onComplete() {
                        dismissProgress();
                        Log.d(TAG, "请求成功");
                    }
                });


    }


    private void login(final String userName) {

        Observable<StateBean> observable = HttpMethods.getInstance().getFyApi().login2(userName);

        observable.subscribeOn(Schedulers.io())//切换到io线程请求网络
                .observeOn(AndroidSchedulers.mainThread())//回到主线程处理请求结果
                .subscribe(new Observer<StateBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "开始获取客户信息");
                        showProgress("请稍后");
                    }

                    @Override
                    public void onNext(final StateBean resBean) {
                        if (resBean.isFlag()) {
                            ToastUtils.showToast("登录成功!");
                            SharedPreferencesUtils.setParam(LoginActivity.this, ZjConfig.userName, userName);
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        } else {
                            ToastUtils.showToast(resBean.getMessage() + "");
                        }
                        Log.e(TAG, "返回数据" + new Gson().toJson(resBean) + "");

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissProgress();
                        Log.e(TAG, "请求失败" + e.getMessage());
                        ToastUtils.showToast("连接失败！" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        dismissProgress();
                        Log.d(TAG, "请求成功");
                    }
                });

    }

}
